/*
 * sc_s.c
 *
 * Created: 2012-07-05 20:06:45
 *  Author: Antrykot
 */ 

#include "../sc-s/main.h"
#include "../sc-s/lcd.h"
#include "../sc-s/sc-s.h"
#include "../sc-s/utils.h"
#include "../sc-s/trans-s.h"
#include <stdlib.h>

u8 kal_f = 0;

u8 cur_pos = 0;

u8 sh_flag = 3;

u16 debug_actref = 33;

void wait_deliver_P()
{
	for (u8 c = 0; c < 200; c++)
	{
		if (tp_addr == 0)
		{
			break;
		}
		WAITMS(5);
	}
}

#ifdef MEDIA_PS
void sr_rt_corr()
{
	lcd_home();
	
	static u8 once = 0;
	
	lcd_home();
	switch(calib_ptr_deep)
	{
		case 0x01:
		{
			lcd_home();

			lcd_print("KOREKTA ");
			once = 0;
			WAITMS(1500);
			calib_ptr_deep++;
			corr_rh = read_word(EEP_RH_CORR);
			corr_t  = read_word(EEP_TM_CORR);
			lcd_clear();
			break;
		}
		
		case 0x02:
		{
			if (!once)
			{
				once = 1;
				lcd_print("  WILG. ");
				WAITMS(1000);
				break;
			}
			
			show_rh();
			
			if (!MIN)
			{
				corr_rh -= 1;
				if (corr_rh  < -30)
				corr_rh = -30;
			}
			if (!PLU)
			{
				corr_rh += 1;
				if (corr_rh  > 30)
				corr_rh = 30;
			}
			if(!ENT)
			{

				write_word(EEP_RH_CORR,corr_rh);
				calib_ptr_deep++;
				while(!ENT);
				once = 0;
				break;
			}
		
			break;
		}		
		case 0x03:
		{
			if (!once)
			{
				once = 1;
				lcd_print("  TEMP. ");
				WAITMS(1000);
				break;
			}
			
			show_tmp();
			
			if (!MIN)
			{
				corr_t -= 1;
				if (corr_t  < -30)
				corr_t = -30;
			}
			if (!PLU)
			{
				corr_t += 1;
				if (corr_t  > 30)
				corr_t = 30;
			}

			if (!ENT)
			{
				while(!ENT);
				WAITMS(1000);
				calib_ptr_deep++;
				write_word(EEP_TM_CORR,corr_t);
				once = 0;
			}
			break;
		}
		case 0x04:
		{
			lcd_print("ZAPISANO");
			WAITMS(1000);
			calib_ptr_deep = 0;
			break;
		}
	}		
		
}
#endif

void serwis_loop()
{
	lcd_home();
	
	static u8 press_delay = 0;
	
	switch (calib_ptr)
	{
		case SM_NR:
		if (calib_ptr_deep)
		{
			sr_nr();
			return;
		}
		lcd_print("   NR   ");
		if (!ENT)
		{
			while(!ENT);
			calib_ptr_deep = 1;
			cur_pos = 0;
			return;
		}
		break;
		
		case SM_AKU:
		if (calib_ptr_deep)
		{
			sr_aku();
			return;
		}
		lcd_print("AKU/ZAS.");
		if (!ENT)
		{
			while(!ENT);
			calib_ptr_deep = 1;
			cur_pos = 0;
			return;
		}
		break;
		
		case SM_DATA:
		if (calib_ptr_deep)
		{
			sr_data();
			return;
		}
		lcd_print("DATA_LEG");
		if (!ENT)
		{
			while(!ENT);
			calib_ptr_deep = 1;
			return;
		}
		break;
		
		case SM_KALU:
		if (calib_ptr_deep)
		{
			sr_kalu();
			return;
		}
		lcd_print("KAL.NAP.");
		if (!ENT)
		{
			while(!ENT);
			calib_ptr_deep = 1;
			return;
		}
		break;
		
		case SM_LOW:
		if (calib_ptr_deep)
		{
			sr_low();
			return;
		}
		lcd_print("KAL.CIS.");
		if (!ENT)
		{
			press_delay ++;
			if (press_delay < 5)
			{
				break;
			}
			
			tp_addr = P_ADDR_L_FACTORY;
			lcd_clear();
			wait_deliver_P();
			
			calib_ptr_deep = 1;

			while(!ENT);			
			WAITMS(300);
			return;
		}
		break;
		
		case SM_HIGH:
		/// TODO : Fix It!
			calib_ptr = SM_CALF;
		if (calib_ptr_deep)
		{
			//sr_rt_corr();
			return;
		}
		lcd_print("RH/T KOR");
		
		if (!ENT)
		{
			calib_ptr_deep++;
		}
		break;
		
		case SM_CALF:
		if (calib_ptr_deep)
		{
			sr_calf();
			return;
		}
		lcd_print("CAL.FREQ");
		if (!ENT)
		{
			while(!ENT);
			calib_ptr_deep = 1;
			return;
		}
		break;
		
		case SM_RESET:

		lcd_print("UST.FAB.");
		if (!ENT)
		{
			lcd_clear();
			for (u8 c = 0; c < 8; c++)
			{
				lcd_print("#");
				WAITMS(330);
				if (ENT)		// key released
				{
					return;
				}					
			}
			tp_addr = P_ADDR_L_FACTORY;
			lcd_home();
			lcd_print("FABRYCZN");
			WAITMS(1000);
			tp_addr = P_ADDR_H_FACTORY;
			WAITMS(1000);
			return;
		}
		break;
		
		case SM_ALDEBUG:
		if (calib_ptr_deep)
		{
			sr_aldebug();
			return;
		}
		lcd_print("AL.CHECK");
		if (!ENT)
		{
			while(!ENT);
			calib_ptr_deep = 1;
			return;
		}
		break;
		
		case SM_OUT:
		lcd_print("WYJSCIE.");
		if (!ENT)
		{
			lcd_clear();
			
			for (u8 c = 0; c < 8; c++)
			{
				lcd_print("#");
				WAITMS(120);
				if (ENT)
					return;
			}			
			if (!ENT)
			{
				Pro = PRO_WORK;
				calib_ptr = 0;
				calib_ptr_deep = 0;
				return;
			}
			return;
		}
		break;
	}	
	
	if (ENT)
	{
		press_delay = 0;
	}
	
	if (!PLU)
	{
		calib_ptr++;
#ifdef MEDIA_PD
		if (calib_ptr == SM_HIGH)
			calib_ptr++;
#endif
		WAITMS(150);	
	}		
	if (!MIN)
	{
		calib_ptr--;
#ifdef MEDIA_PD
		if (calib_ptr == SM_HIGH)
			calib_ptr--;
#endif
		WAITMS(150);
	}
	
	if (calib_ptr > SM_OUT)
		calib_ptr = 0x00;
		
	if (calib_ptr < 0x00)
	{
		calib_ptr = SM_OUT;
	}

}



void disp_nr(i8 a,i8 b, i8 c, i8 d, u8 cur)
{
		static u8 blink = 0;
		
		char str[6];
		
		lcd_home();
		lcd_print("NR:");
		
		
		str[0] = a;
		str[1] = b;
		str[2] = c;
		str[3] = d;
		str[4] = 0;
		
		lcd_print(str);

		
		blink++;
		if (blink > 3)
		{
			blink = 0;
			lcd_setpos(6-cur);
			lcd_print("#");
		}
}

void sr_nr()
{
	i8 a,b,c,d;
	
	a = ( ( Nr / 1000 ) % 10) + '0';
	b = ( ( Nr / 100 ) % 10) + '0';
	c = ( ( Nr / 10 ) % 10) + '0';
	d = ( Nr % 10) + '0';
	
	disp_nr(a,b,c,d,cur_pos);
	
	
	if (!ENT)
	{
		cur_pos++;
		if (cur_pos > 3)
		{
			if (Nr > 4096)
			{
				lcd_home();
				lcd_print("!!BLAD!!");
				WAITMS(3500);
				calib_ptr_deep = 0;
				return;
			}
			
			write_word(EEP_NR,Nr);
			tk_data = Nr;
			tk_addr = K_ADDR_NR;
			lcd_home();
			lcd_print("ZAPIS");
			WAITMS(500);
			calib_ptr_deep = 0;
		}
		while(!ENT);
	}
	i8 *ptr = 0;
	
	if (!PLU || !MIN)
	{
		switch (cur_pos)
		{
			case 0:
			ptr = &d;
			break;
			case 1:
			ptr = &c;
			break;
			case 2:
			ptr = &b;
			break;
			case 3:
			ptr = &a;
			break;
		}	
		if (!PLU)
		(*ptr) ++;
		if (!MIN)
		(*ptr)--;
		
		if ((*ptr) > '9')
		{
			*ptr = '0';
		}
		if ((*ptr) < '0')
		{
			*ptr = '9';
		}
		Nr = (a - '0') * 1000 + (b - '0') * 100 + (c - '0') * 10 + (d - '0');
	}
		
}


void sr_aku()
{
	/// used cur_pos here !
	char s[10];
	lcd_home();
	if (calib_ptr_deep == 1)
	{
		lcd_print("A=");
		itoa(v_aku,s,10);
	}
	if (calib_ptr_deep == 2)
	{
		lcd_print("Z=");
		itoa(v_pow,s,10);
	}
	lcd_print(s);
	lcd_print("  ");
	
	if (!ENT)
	{
		while(!ENT);
		if (calib_ptr_deep == 1)
		{
			calib_ptr_deep = 2;
			return;
		}	
		lcd_home();
		lcd_print("ZAPISANO")	;
		WAITMS(2000);
		calib_ptr_deep = 0;
		return;	
	}
	if (!PLU || !MIN)
	{
		cur_pos++;
	}
	else
	{
		cur_pos = 0;
	}
	
	
	if (!PLU)
	{
		cli();
		tp_addr = P_ADDR_INCAKU;
		if (calib_ptr_deep == 2)
		{
			tp_addr += 2;		// INC POW
		}
		if (cur_pos > 20)
			tp_addr += 4;		// inc more
		sei();
	}
	
	if (!MIN)
	{
		cli();
		tp_addr = P_ADDR_DECAKU;
		if (calib_ptr_deep == 2)
		{
			tp_addr += 2;	// DEC_POW
		}
		if (cur_pos > 20)
			tp_addr += 4;		// dec more
		sei();
	}
	
}


void sr_data()
{
	u8 y,m,d;
	y = leg_date / 512;
	m = ( leg_date - y * 512) / 32;
	d = (leg_date - y * 512 - m * 32);
	
	char s[10];
	lcd_home();
	
	switch (calib_ptr_deep)
	{
		case 0x01:
		{
			lcd_print("ROK=");
			itoa(y,s,10);
			if (!PLU)
			{
				y ++;
				if (y > 20)
				y = 11;
			}
			if (!MIN)
			{
				y --;
				if (y < 10)
				y = 10;
			}
			break;
		}
		case 0x02:
		{
			lcd_print("MIES=");
			itoa(m,s,10);
			if (!PLU)
			{
				m ++;
				if (m > 12)
				m = 1;
			}
			if (!MIN)
			{
				m --;
				if (m < 1)
				m = 12;
			}
			break;
		}
		case 0x03:
		{
			lcd_print("DZIEN=");
			itoa(d,s,10);
			if (!PLU)
			{
				d ++;
				if (d > 31)
				d = 1;
			}
			if (!MIN)
			{
				d --;
				if (d < 1)
				d = 31;
			}
			break;
		}

	}
	lcd_print(s);
	lcd_print("  ");
	
	leg_date = y * 512 + m * 32 + d ;
	
	if (!ENT)
	{
		calib_ptr_deep++;
		if (calib_ptr_deep > 3)
		{
			lcd_home();
			lcd_print("ZAPISANO");
			write_word(EEP_DATE,leg_date);
			tk_data = leg_date;
			tk_addr = K_ADDR_DATE;
			WAITMS(1500);
			calib_ptr_deep = 0;
			return;
		}
		while(!ENT);
	}
	
	
}


void sr_kalu()
{
	lcd_home();
	uout = 2;
	switch (calib_ptr_deep)
	{
		case 0x01:
		{
			lcd_print("U1_350  ");
			if (!PLU)
			{
				U1_35++;
			}
			if (!MIN)
			{
				U1_35--;
			}
			napa = U1_35;
			break;
		}
		case 0x02:
		{
			lcd_print("U1_400  ");
			if (!PLU)
			{
				U1_40++;
			}
			if (!MIN)
			{
				U1_40--;
			}
			napa = U1_40;
			break;
		}
		case 0x03:
		{
			lcd_print("U1_2000 ");
			if (!PLU)
			{
				U1_20++;
			}
			if (!MIN)
			{
				U1_20--;
			}
			napa = U1_20;
			break;
		}
		case 0x04:
		{
			lcd_print("U1_2100 ");
			if (!PLU)
			{
				U1_21++;
			}
			if (!MIN)
			{
				U1_21--;
			}
			napa = U1_21;
			break;
		}
		case 0x05:
		{
			lcd_print("U2_350  ");
			if (!PLU)
			{
				U2_35++;
			}
			if (!MIN)
			{
				U2_35--;
			}
			napb = U2_35;
			break;
		}
		case 0x06:
		{
			lcd_print("U2_400  ");
			if (!PLU)
			{
				U2_40++;
			}
			if (!MIN)
			{
				U2_40--;
			}
			napb = U2_40;
			break;
		}
		case 0x07:
		{
			lcd_print("U2_2000 ");
			if (!PLU)
			{
				U2_20++;
			}
			if (!MIN)
			{
				U2_20--;
			}
			napb = U2_20;
			break;
		}
		case 0x08:
		{
			lcd_print("U2_2100 ");
			if (!PLU)
			{
				U2_21++;
			}
			if (!MIN)
			{
				U2_21--;
			}
			napb = U2_21;
			break;
		}
	}
	
	if (!ENT)
	{
		calib_ptr_deep ++;
		if (calib_ptr_deep > 0x08)
		{
			write_word(EEP_1_21,U1_21);
			write_word(EEP_1_20,U1_20);
			write_word(EEP_1_40,U1_40);
			write_word(EEP_1_35,U1_35);

			write_word(EEP_2_21,U2_21);
			write_word(EEP_2_20,U2_20);
			write_word(EEP_2_40,U2_40);
			write_word(EEP_2_35,U2_35);
			lcd_home();
			lcd_print("ZAPISANO");
			WAITMS(2000);
			calib_ptr_deep = 0;
			uout = _read(EEP_UOUT);
			
			
			
			return;
		}
		while(!ENT);
	}

}


void sr_calf()
{
	
	lcd_home();
	
	lcd_print("FRQ:");
	char s[6];
	itoa(kal_f,s,10);
	lcd_print(s);
	lcd_print("   ");
	
	if (!PLU)
	{
		tk_addr = K_ADDR_CALFP;
	}
	if (!MIN)
	{
		tk_addr = K_ADDR_CALFM;
	}
	if (!ENT)
	{
		lcd_home();
		lcd_print("ZAPISANO");
		WAITMS(2000);
		calib_ptr_deep = 0;
	}

}


void sr_high()
{
	lcd_home();
	if (meas & 0x4000)
	{
		lcd_print("ERR!    ");
		WAITMS(1000);
		calib_ptr_deep = 0;
		return;
	}
	if (!MIN && ( (calib_ptr_deep != 2) && (calib_ptr_deep != 4) ))
	{
		lcd_home();
		lcd_print("...     ");
		calib_ptr_deep = 0;
		tp_addr = P_ADDR_RESET_DEFAULTS;
		WAITMS(1000);
		tp_addr = P_ADDR_SW_LRANGE;
		WAITMS(1000);
		return;
	}
	
	switch(calib_ptr_deep)
	{
		case 0x01:
		{
			lcd_print("POW:");
			//show_med_high(meas & 0x3FFF);
			lcd_print("  ");
			if (!ENT)
			{
				tp_addr = P_ADDR_SCALHZ;
				lcd_home();
				lcd_print("Zapisano");
				WAITMS(1000);
				calib_ptr_deep++;
				calib_gas = read_word(EEP_STW1);
			}
			break;
		}
		case 0x02:
		{
			lcd_print("G1 :");
			//show_med_high(calib_gas);
			lcd_print("  ");
			if (MIN && PLU)
			{	
				cur_pos = 0;
			}
			if (!MIN)
			{
				cur_pos ++;
				if (cur_pos > 5)
				{
					cur_pos = 5;
					calib_gas -= 9;
				}
				
				calib_gas --;
				if (calib_gas < 170)
				calib_gas = 170;
			}
			if (!PLU)
			{
				cur_pos ++;
				if (cur_pos > 5)
				{
					cur_pos = 5;
					calib_gas += 9;
				}
				calib_gas ++;
				if (calib_gas > 500)
				calib_gas = 500;
			}
			if(!ENT)
			{
				write_word(EEP_STW1,calib_gas);
				calib_ptr_deep++;
				while(!ENT);
				break;
			}
			
			break;
		}
		case 0x03:
		{
			lcd_print("W1:");
			//show_med_high(meas & 0x3FFF);
			lcd_print("  ");
			if (!ENT)
			{
				tp_data = calib_gas;
				tp_addr = P_ADDR_SCALHS1;
				lcd_home();
				lcd_print("UST.GAZ ");
				WAITMS(1000);
				calib_ptr_deep ++;
				calib_gas = read_word(EEP_STW2);

				return;
			}
			break;
		}
		case 0x04:
		{
			lcd_print("G2 :");
			//show_med_high(calib_gas);
			lcd_print("  ");
			if (MIN && PLU)
			{
				cur_pos = 0;
			}
			
			if (!MIN)
			{
				cur_pos++;
				if (cur_pos > 5)
				{
					cur_pos = 5;
					calib_gas -= 9;
				}
				
				calib_gas --;
				if (calib_gas < 510)
				calib_gas = 510;
			}
			if (!PLU)
			{
				cur_pos++;
				if (cur_pos > 5)
				{
					cur_pos = 5;
					calib_gas += 9;
				}
				
				calib_gas ++;
				if (calib_gas > 999)
				calib_gas = 999;
			}
			if(!ENT)
			{
				write_word(EEP_STW2,calib_gas);
				calib_ptr_deep++;
				while(!ENT);
				break;
			}
			
			break;
		}
		case 0x05:
		{
			lcd_print("W2:");
			//show_med_high(meas & 0x3FFF);
			lcd_print("  ");
			if (!ENT)
			{
				tp_data = calib_gas;
				tp_addr = P_ADDR_SCALHS2;
				lcd_home();
				lcd_print("Zapisano");
				wait_deliver_P();
				WAITMS(1000);
				tp_addr = P_ADDR_SW_LRANGE;
				WAITMS(1000);
				calib_ptr_deep = 0;
				return;
			}
			break;
		}

	}
	
	
}

#ifdef MEDIA_CH4
#define SG_LE 170
#define SG_HE 240
#elif MEDIA_CON
#define SG_LE 130
#define SG_HE 195
#elif MEDIA_COS
#define SG_LE 700
#define SG_HE 980
#elif MEDIA_CO2
#define SG_LE 160
#define SG_HE 280
#elif MEDIA_PS
#define SG_LE 160
#define SG_HE 280
#elif MEDIA_PD
#define SG_LE 160
#define SG_HE 280
#endif

#ifdef MEDIA_PS
void sr_low()
{
	lcd_home();


	static u16 low_press_limit = 0;
	
	static u8 once = 0;
	
	lcd_home();
	switch(calib_ptr_deep)
	{
		case 0x01:
		{
			lcd_home();
			tp_addr = P_ADDR_L_FACTORY;
			tp_data = 0;
			
			lcd_print("ABSOLUTN");
			once = 0;
			WAITMS(1500);
			calib_ptr_deep++;
			calib_gas = read_word(EEP_SPRN);
			lcd_clear();
			break;
		}
		
		case 0x02:
		{
			if (!once)
			{
				once = 1;
				lcd_def(1,0b00100,0b00100,0b01110,0b10001,0b10001,0b10001,0b10001,0b01110);
				lcd_clear();
				lcd_print("WZ");
				lcd_sign(1);
				lcd_print("R NIS");
				WAITMS(1000);
				break; 
			}
			
			lcd_print("WZ:");
			pom_pr = calib_gas;
			show_pr_spec();
			
			if (!MIN)
			{
				calib_gas -= 10;
				if (calib_gas < 8000)
				calib_gas = 8000;
			}
			if (!PLU)
			{
				calib_gas += 10;
				if (calib_gas > 13000)
				calib_gas = 13000;
			}
			if(!ENT)
			{
				low_press_limit = calib_gas;
				write_word(EEP_SPRN,calib_gas);
				calib_ptr_deep++;
				while(!ENT);
				once = 0;
				break;
			}
			
			break;

		}
		case 0x03:
		{
			if (!once)
			{
				once = 1;
				lcd_print("POMIAR N");
				WAITMS(1000);
				break;
			}
			
			lcd_print("PN:");
			show_pr_spec();
			lcd_print("  ");
			if (!ENT)
			{
				tp_data = calib_gas;
				tp_addr = P_ADDR_SCALLZ;
				lcd_home();
				lcd_print("C.NISKIE");
				while(!ENT);
				WAITMS(1000);
				calib_ptr_deep++;
				calib_gas = read_word(EEP_SPRW);
				once = 0;
			}
			break;		
		}
		case 0x04:
		{
			if (!once)
			{
				once = 1;
				lcd_def(1,0b00100,0b00100,0b01110,0b10001,0b10001,0b10001,0b10001,0b01110);
				lcd_clear();
				lcd_print("WZ");
				lcd_sign(1);
				lcd_print("R WYS");
				WAITMS(1000);
				break;
			}

			lcd_print("WW:");
			pom_pr = calib_gas;
			show_pr_spec();
			lcd_print("  ");
			
			
			
			if (!MIN)
			{
				calib_gas -= 10;
				if (calib_gas < low_press_limit + 300)
				calib_gas = low_press_limit + 300;
			}
			if (!PLU)
			{
				calib_gas += 10;
				if (calib_gas > 14000)
				calib_gas = 14000;
			}
			if(!ENT)
			{
				write_word(EEP_SPRW,calib_gas);
				calib_ptr_deep++;
				while(!ENT);
				once = 0;
				break;
			}
			break;
		}
		case 0x05:
		{
			if (!once)
			{
				once = 1;
				lcd_home();
				lcd_print("POMIAR W");
				WAITMS(1000);
				break;
			}

			lcd_print("PW:"); 
			show_pr_spec();
			lcd_print("  ");
			if (!ENT)
			{
				tp_data = calib_gas;
				tp_addr = P_ADDR_SCALLS;
				lcd_home();
				lcd_print(" USTAW. ");
				while(!ENT);
				WAITMS(1000);
				calib_ptr_deep = 0;
			}
			break;
		}
	}
}
#elif MEDIA_PD
void sr_low()
{
	lcd_home();

	static u8 once = 0;

	static u16 low_press_limit = 0;
	
	switch(calib_ptr_deep)
	{
		case 0x01:
		{
			lcd_home();
			lcd_print("ROZNICOW");
			tp_addr = P_ADDR_L_FACTORY;
			tp_data = 0;
			WAITMS(1500);
			calib_ptr_deep++;
			break;
		}

		case 0x02:
		{
			if (!once)
			{
				once = 1;
				lcd_print("  ZERO  ");
				WAITMS(1000);
				break;
			}
			lcd_print("ZE:");
			show_prd_spec();
			lcd_print("  ");
			if (!ENT)
			{
				tp_data = 0;
				tp_addr = P_ADDR_SCALLZ;
				lcd_home();
				lcd_print("UST.ZERO");
				while(!ENT);
				WAITMS(1000);
				calib_ptr_deep++;
				calib_gas = read_word(EEP_SPRW);
				once = 0;
			}
			break;
		}
		case 0x03:
		{
			if (!once)
			{
				once = 1;
				lcd_def(1,0b00100,0b00100,0b01110,0b10001,0b10001,0b10001,0b10001,0b01110);
				lcd_print("WZ");
				lcd_sign(1);
				lcd_print("R CIS");
				WAITMS(1000);
				break;
			}
			lcd_print("WW:");
			pom_pr = calib_gas;
			show_prd_spec();
			lcd_print("  ");
			
			if (!MIN)
			{
				calib_gas -= 10;
				if (calib_gas < 100)
				calib_gas = 100;
			}
			if (!PLU)
			{
				calib_gas += 10;
				if (calib_gas > 1000)
				calib_gas = 1000;
			}
			if(!ENT)
			{
				write_word(EEP_SPRW,calib_gas);
				calib_ptr_deep++;
				while(!ENT);
				once = 0;
				break;
			}
			break;
		}
		case 0x04:
		{
			if (!once)
			{
				once = 1;
				lcd_print("POMIAR W");
				WAITMS(1000);
				break;
			}

			show_prd_spec(); 
			lcd_print("  ");
			if (!ENT)
			{
				tp_data = calib_gas;
				tp_addr = P_ADDR_SCALLS;
				lcd_home();
				lcd_print("UST.CZUL");
				while(!ENT);
				WAITMS(1000);
				calib_ptr_deep = 0;
			}
			break;
		}
	}
}
#endif

void sr_aldebug()
{
	
	lcd_home();
	
	lcd_print("AL:");
	char s[6];
	itoa(debug_data,s,10);
	lcd_print(s);
	lcd_print("      ");

	tk_addr = K_ADDR_DEBUG_AL;

	if (!ENT)
	{
		lcd_home();
		lcd_print("KONIEC");
		WAITMS(2000);
		calib_ptr_deep = 0;
		debug_data = 0;
	}

}


void sr_actref()
{
	char str[10];
	lcd_home();
	switch(cur)
	{
		case 0:
		{
			lcd_print("ACT:");
			tp_addr = P_ADDR_GETACT;
			wait_deliver_P();
			break;
	
		}
		case 1:
		{
			lcd_print("REF:");
			tp_addr = P_ADDR_GETREF;
			wait_deliver_P();
			break;
		}
		case 2:
		{
			lcd_print("BETA[+-]");
			tp_addr = P_ADDR_GET_TCAL;
			WAITMS(1000);
			cur ++;
			break;
		}
		case 3:
		{
			//tp_addr = P_ADDR_GET_TCAL;
			lcd_print("TC:");
			if (debug_data & (1 << 15))
			{
				debug_data &= ~(1 << 15);
				lcd_print("-");
			}
			fuse1(str,debug_data);
			lcd_print(str);
			lcd_print("   ");
			WAITMS(2000);
			cur ++;

		}
		case 4:
		{
			lcd_print("TA:");
			if (v_tmp & (1 << 15))
			{
				lcd_print("-");
			}
			fuse1(str,v_tmp & 0x7FFF);
			lcd_print(str);
			lcd_print("   ");

		}
		case 5:
		{
			tp_addr = P_ADDR_GET_BETA;
			//show_med_low(meas);
			lcd_print("/");
			itoa(debug_data,str,10);
			lcd_print(str);
			lcd_print("   ");
			if (!PLU)
			{
				tp_addr = P_ADDR_INCR_BETA;
			}
			if (!MIN)
			{
				tp_addr = P_ADDR_DECR_BETA;
			}
			
		}		
	}
	
	
		
	char s[6];
	itoa(debug_actref,s,10);
	lcd_print(s);
	lcd_print("   ");


	if (!ENT)
	{
		if (cur < 5)
		{
			cur ++;
			while(!ENT);
			return;
		}
		
		lcd_home();
		lcd_print(" KONIEC ");
		WAITMS(2000);
		calib_ptr_deep = 0;
		debug_data = 0;
		cur = 0;
	}

}